<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\common\PayUtil;
use app\logic\CommonLogic;
use app\logic\GoodsLogic;
use app\logic\PlayGoodsLogic;
use app\logic\ShopLogic;
use app\models\Area;
use app\models\Cart;
use app\models\OrderGoods;
use Yii;


class IndexController extends BaseController
{

    const LIMIT = 10;
	public function actionIndex()
	{
	    /*$pu = new PayUtil();
	    $res = $pu->getUrl('2018072755100999','20180728132900',0.01,'test');
	    header("Location:$res");exit;*/
        $this->response(time());
	}

    /**
     * 搜索
     * @apiDescription   by Jim
     * @api              {post} /index/search 搜索
     * @apiName          search
     * @apiVersion       1.0.0
     * @apiParam {string} keyword 搜索关键词
     * @apiParam {int} orderBy 排序 总共有四种， 1 综合 2 销量高到低  3 价格低到高  4 价格高到低，默认是综合
     * @apiParam {int} category_id 分类ID，传空或传0为全部分类
     * @apiParam {int} page 页码，默认为1
     * @apiGroup         Index
     * @apiSampleRequest /index/search?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
        "goods_id": 12, // int 商品Id
        "goods_pic": "https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3776766248,514215818&fm=173&app=25&f=JPEG?w=550&h=367&s=B9248F180C1448CE1E9870C00300F0A9", // 商品图片
        "goods_name": "商品标题商品标题商品标题", // 商品标题
        "goods_price": 12.00 // float 商品价格
      }]
     * @apiError         10003    获取失败
     */
    public function actionSearch(){
        $keyword = $this->getQueryparams('keyword',"");
        $orderBy = $this->getQueryparams('orderBy',1);
        $category_id = $this->getQueryparams('category_id',0);
        $page = $this->getQueryparams('page',1);
        $goodsLogic = new GoodsLogic();
        $result = $goodsLogic->search($keyword,$orderBy,$category_id,$page,self::LIMIT);
        foreach($result AS &$item){
            $item['goods_pic'] = Yii::$app->params['pic_domain'].$item['goods_pic'];
        }
        $this->response($result);
    }

    /**
     * 获取广告
     * @apiDescription   by Jim
     * @api              {post} /index/get-banner 获取广告
     * @apiName          get-banner
     * @apiVersion       1.0.0
     * @apiGroup         Index
     * @apiSampleRequest /index/get-banner?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
        "adv_link": "http://xxxx", // 广告对应的链接地址
        "adv_pic": "xxx", // 广告对应的图片地址
        "adv_title": "xxx" // 广告对应的标题
     }]
     * @apiError         10000
     */
    public function actionGetBanner()
    {
        $commonLogic = new CommonLogic();
        $adv = $commonLogic->getAdv();
        foreach($adv AS &$item){
            $item['adv_pic'] = Yii::$app->params['pic_domain'].$item['adv_pic'];
        }
        $this->response($adv);
    }

    /**
     * 人气推荐
     * @apiDescription   by Jim
     * @api              {post} /index/hot-recommend 人气推荐
     * @apiName          hot-recommend
     * @apiVersion       1.0.0
     * @apiGroup         Index
     * @apiSampleRequest /index/hot-recommend?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
    "goods_id": 12, // int 商品Id
    "goods_pic": "https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3776766248,514215818&fm=173&app=25&f=JPEG?w=550&h=367&s=B9248F180C1448CE1E9870C00300F0A9", // 商品图片
    "goods_name": "商品标题商品标题商品标题", // 商品标题
    "goods_price": 12.00 // float 商品价格
    }]
     * @apiError         10001    获取失败
     */
    public function actionHotRecommend(){
        $goodsLogic = new GoodsLogic();
        $goods = $goodsLogic->getAllGoodsByHot();
        foreach($goods AS &$item){
            $item['goods_pic'] = Yii::$app->params['pic_domain'].$item['goods_pic'];
        }
        $this->response($goods);
    }

    /**
     * 新品推荐
     * @apiDescription   by Jim
     * @api              {post} /index/new-recommend 人气推荐
     * @apiName          new-recommend
     * @apiVersion       1.0.0
     * @apiParam {int} page 页码，默认为1
     * @apiGroup         Index
     * @apiSampleRequest /index/new-recommend?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
    "goods_id": 12, // int 商品Id
    "goods_pic": "https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3776766248,514215818&fm=173&app=25&f=JPEG?w=550&h=367&s=B9248F180C1448CE1E9870C00300F0A9", // 商品图片
    "goods_name": "商品标题商品标题商品标题", // 商品标题
    "goods_price": 12.00 // float 商品价格
    }]
     * @apiError         10002    获取失败
     */
    public function actionNewRecommend(){
        $page = $this->getQueryparams('page',1);
        $goodsLogic = new GoodsLogic();
        $goods = $goodsLogic->getAllGoodsByNew($page,self::LIMIT);
        //$goods = $goodsLogic->getAllGoods($page,self::LIMIT);
        foreach($goods AS &$item){
            $item['goods_pic'] = Yii::$app->params['pic_domain'].$item['goods_pic'];
        }
        $this->response($goods);
    }

    public function actionGetArea(){
        $area = Area::find()->where(['region_level'=>3])->asArray()->all();
        $result1 = [];
        foreach($area AS $key=>$value){
            $result1[$value['parent_code']]['children'][] = ['region_id'=>$value['region_code'],'region_name'=>$value['region_name']];
        }
        $result2 = [];
        $area = Area::find()->where(['region_level'=>2])->asArray()->all();
        foreach($area AS $key=>$value){
            $result1[$value['region_code']]['region_name'] = $value['region_name'];
            $result1[$value['region_code']]['region_id'] = $value['region_code'];
            $result2[$value['parent_code']]['children'][] = $result1[$value['region_code']];
        }
        $area = Area::find()->where(['region_level'=>1])->asArray()->all();
        foreach($area AS $key=>$value){
            $result2[$value['region_code']]['region_name'] = $value['region_name'];
            $result2[$value['region_code']]['region_id'] = $value['region_code'];
        }
        $result = (array_values($result2));
        $this->response($result);
    }

}
